package com.hl.card.config;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.hl.card.common.persistence.dao.SysDicMapper;
import com.hl.card.common.persistence.dao.SysDicTypeMapper;
import com.hl.card.common.persistence.model.SysDic;
import com.hl.card.common.persistence.model.SysDicType;


/**
 *  初始化字典
 * @author 袁伟
 *
 */
@Component
@Order(1)
public class DictConfig implements CommandLineRunner{

	@Autowired
	private RedisTemplate<String, Object>  redisTemplate;
	@Autowired
	private SysDicTypeMapper sysDicTypeMapper;
	@Autowired
	private SysDicMapper sysDicMapper;
	
	public static String KEY="redis_key_dict:";
	

	@Override
	public void run(String... args) throws Exception {
		System.out.println("初始化字典");
		List<SysDicType> dicTypeList= sysDicTypeMapper.selectList(null);
		List<SysDic>  dicList= sysDicMapper.selectList(null);
		dicTypeList.forEach((dicType)->
		{
			List<SysDic> filterList = dicList.stream().filter(dic -> dic.getDicTypeNo().equals(dicType.getDicTypeNo())).collect(Collectors.toList());
			filterList.forEach((l)->{
				redisTemplate.opsForHash().put(KEY+dicType.getDicTypeNo(), l.getDicNo(), l.getDicValue());
			});

		});
		
		
	}

}
